/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package jp.co.irep.juten.facade;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import jp.co.irep.juten.entity.Place;
import jp.co.irep.juten.entity.PlaceProduct;

/**
 *
 * @author toan_nguyen
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class PlaceFacade extends AbstractFacade<Place> {
    @PersistenceContext(unitName = "JutenPU")
    private EntityManager em;

    @Resource
    private UserTransaction userTransaction;
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserTransaction getUserTransaction() {
        return userTransaction;
    }

    
    
    public PlaceFacade() {
        super(Place.class);
    }

    public List<Place> selectPlaceByHashMap(Map<String, Object> placeMap) {
//        getEntityManager().clear();
        
        CriteriaQuery query = getEntityManager().getCriteriaBuilder().createQuery();
        Root<Place> place = query.from(Place.class);
        query.select(place); 
        
       
        
        List<Predicate> predicateList = new ArrayList<Predicate>();
        for(Map.Entry<String,Object> entry : placeMap.entrySet())
        {
            if(entry.getValue()!=null)
            {
                if(entry.getKey().equals("placeName") && !entry.getValue().equals(""))
                {
                    //query.where(getEntityManager().getCriteriaBuilder().like(place.<String>get("name"), "%"+entry.getValue()+"%"));
                    predicateList.add(getEntityManager().getCriteriaBuilder().like(place.<String>get("name"), "%"+entry.getValue()+"%"));
                }
                if(entry.getKey().equals("prefectureId") && (int)entry.getValue()>0)
                {
                    //query.where(getEntityManager().getCriteriaBuilder().equal(place.get("prefectureId"), entry.getValue()));
                    predicateList.add(getEntityManager().getCriteriaBuilder().equal(place.get("prefecture").get("id"), entry.getValue()));
                }
            }
        }
        
        if(!predicateList.isEmpty())
        {
            Predicate[] predicates = new Predicate[predicateList.size()];
            predicateList.toArray(predicates);
            query.where(predicates);
        }
        query.orderBy(getEntityManager().getCriteriaBuilder().asc(place.get("id")));
        List<Place> list = null;
        try{
             getUserTransaction().begin();
             list = getEntityManager().createQuery(query).getResultList();
             getUserTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
        }
                
        return list;
    }

    public List<PlaceProduct> selectPlaceProductList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    @Override
    public void edit(Place place){
        
        try{
            getUserTransaction().begin();
//            Place pl = getEntityManager().find(Place.class, place.getId());
//            pl.setName(place.getName());
//            pl.setDate(place.getDate());
//            pl.setMemo(place.getMemo());
//            pl.setPrefecture(place.getPrefecture());
//            pl.setDeleteFlg(place.getDeleteFlg());
            getEntityManager().merge(place);
//            getEntityManager().flush();
            getUserTransaction().commit();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
}
